package com.esm.sm800.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.esm.sm800.R;
import com.esm.sm800.database.manager.MemberCenter;
import com.esm.sm800.database.manager.SystemCenter;
import com.esm.sm800.sm800floating.GlobalUtil;
import com.esm.sm800.sm800floating.browserReport;
import com.esm.sm800.sm800floating.playVidioMp3;
import com.esm.sm800.util.Camera2Manager;
import com.esm.sm800.util.Utils;
import com.esm.sm800.util.ZazManager;

import java.io.File;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.ui.AlertDialog;

public class StszActivity extends BaseActivity implements View.OnClickListener, AlertDialog.OnDialogButtonClickListener {

    private final String TAG = "StszActivity";

    private TextView tv_bt1;
    private TextView tv_bt2;
    private TextView tv_bt3;

    private TextView tv_bt4;
    private TextView tv_bt5;
    private TextView tv_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_stsz);

        tv_bt1 = (TextView) findViewById(R.id.tv_bt1);
        tv_bt2 = (TextView) findViewById(R.id.tv_bt2);
        tv_bt3 = (TextView) findViewById(R.id.tv_bt3);
        tv_bt4 = (TextView) findViewById(R.id.tv_bt4);
        tv_bt5 = (TextView) findViewById(R.id.tv_bt5);
        tv_back = (TextView) findViewById(R.id.tv_back);

        findViewById(R.id.tv_bt1).setOnClickListener(this);
        findViewById(R.id.tv_bt2).setOnClickListener(this);
        findViewById(R.id.tv_bt3).setOnClickListener(this);
        findViewById(R.id.tv_bt4).setOnClickListener(this);
        findViewById(R.id.tv_bt5).setOnClickListener(this);
        findViewById(R.id.tv_bt6).setOnClickListener(this);
        findViewById(R.id.tv_back).setOnClickListener(this);

        findViewById(R.id.tv_stsz).setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public static final int REFRESH_UI = 1101;
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == REFRESH_UI) {

            }
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.tv_bt1:
                onBt1();
                break;
            case R.id.tv_bt2:
                onBt2();
                break;
            case R.id.tv_bt3:
                onBt3();
                break;
            case R.id.tv_bt4:
                onBt4();
                break;
            case R.id.tv_bt5:
                onBt5();
                break;
            case R.id.tv_bt6:
                onBt6();
                break;
            case R.id.tv_back:
                finish();
                break;
            case R.id.tv_stsz:
                onHy();
                break;
        }
    }

    int _hy = 0;

    //恢复出厂设置
    private void onHy() {
        if (SystemCenter.getInstance().member.getType() > 0 || !SystemCenter.getInstance().member.getName().equals("admin")) {
            //必需是总管理员账号--admin账号
            return;
        }
        _hy++;
        if (_hy == 5) {
            new AlertDialog(context, "", "是否退回桌面？", true, 17, this).show();
        }
        if (_hy == 10) {
            _hy = 0;
            new AlertDialog(context, "", getString(R.string.qqdsfhfccsz), true, 13, this).show();
        }

    }

    private void doHy() {
        //删除数据库--如删除文件则要重启
        SystemCenter.getInstance().clearDB();

        //清空指纹
        ZazManager.getInstance().deleteAll();

        //删图片文件夹
        String path = Camera2Manager.get().getFilesPath(context) + "/ESMJPG/";
        Utils.deleteDir(path);

        String path1 = Camera2Manager.get().getFilesPath(context) + "/ZZJPG/";
        Utils.deleteDir(path1);

    }

    private void onBt1() {
        Intent intent = new Intent(this, ZzglActivity.class);
        startActivityForResult(intent, 177);

    }

    private void onBt2() {
        Intent intent = new Intent(this, RyglActivity.class);
        startActivity(intent);
    }

    private void onBt3() {
        Intent intent = new Intent(this, SbglActivity.class);
        startActivity(intent);
    }

    private void onBt4() {
        Intent intent = new Intent(this, CsszActivity.class);
        startActivityForResult(intent, 212);
    }

    private void onBt5() {
        Intent intent = new Intent(this, browserReport.class);
        startActivity(intent);
    }

    private void onBt6() {
        String currentLanguage = GlobalUtil.getCurrentLanguage(getBaseContext());
        ///mnt/sdcard/Download/usbCameraDual.apk
        File file = null;
        String mp3FullPath = "";
        if (currentLanguage.equals("zh")) {
            mp3FullPath = GlobalUtil._zh_HelpMp3Path;
        } else if (currentLanguage.equals("vi")) {
            mp3FullPath = GlobalUtil._vi_HelpMp3Path;
        } else {
            mp3FullPath = GlobalUtil._en_HelpMp3Path;
        }
        // 文件存在吗？
        file = new File(mp3FullPath);
        if (!file.exists()) {
            // 文件不存在
            Toast.makeText(StszActivity.this, getString(R.string.err_file_noexist), Toast.LENGTH_SHORT).show();
            return;
        }
        Intent intentPlay = new Intent(StszActivity.this, playVidioMp3.class);
        intentPlay.putExtra("mp3FilePath", mp3FullPath); // 添加参数
        startActivity(intentPlay);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 666) {
            setResult(666);
            finish();
            return;
        }
        if (resultCode == 777) {
            setResult(777);
            finish();
            return;
        }
        if (resultCode != RESULT_OK) {
            return;
        }
    }


    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {

    }

    @Override
    public void onDialogButtonClick(int requestCode, boolean isPositive) {
        if (isPositive == false) {
            return;
        }
        if (requestCode == 13) {
            //确定恢复出厂设置
            doHy();
            setResult(666);
            finish();
        } else if (requestCode == 17) {
            setResult(999);
            finish();
        }
    }
}
